package com.lgkj.ui;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.adapter.CityChoiceAdapter;
import com.lgkj.bean.CityBean;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/12/5 0005.
 */

public class ChoiceCity extends BaseActivity implements AdapterView.OnItemClickListener {
    public static String cityId = "0";
    public static String province_id = "0";
    public static String cityText = "";
    @BindView(R.id.lv_list)
    ListView lvList;
    private CityChoiceAdapter adapter;
    private CityBean countybean, cityBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_choice_city;
    }

    @Override
    public void initView() {
        setTitle("选择城市");
        cityId = "";
        province_id = "";
        cityText = "";
    }

    @Override
    public void initData() {
        adapter = new CityChoiceAdapter(ChoiceCity.this);
        lvList.setAdapter(adapter);
        lvList.setOnItemClickListener(this);
        getcounty();
    }

    /**
     *
     */
    private void getcounty() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        HttpUtils.postHttp(map, CommonConstant.getProvinceAll, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    countybean = mGson.fromJson(String.valueOf(jsonObject), CityBean.class);
                    adapter.setBeans(countybean.getData());
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }

    /**
     *
     */
    private void getcity(String province_id) {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("province_id", province_id);
        HttpUtils.postHttp(map, CommonConstant.getCityByProvince, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    cityBean = mGson.fromJson(String.valueOf(jsonObject), CityBean.class);
                    adapter.setBeans(cityBean.getData());
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CityBean.DataBean dataBean = adapter.getItem(position);
        if (dataBean.getLevel().equals("1")) {
            getcity(dataBean.getId());
            province_id = dataBean.getId();
            cityText = dataBean.getName() + "-";
        } else {
            cityText += dataBean.getName();
            cityId = dataBean.getId();
            finish();
        }
    }

}
